package com.soft.mainsite.module.wx.controller;

import com.github.pagehelper.PageInfo;
import com.soft.mainsite.common.utils.ExcelUtil;
import com.soft.mainsite.module.wx.entity.WeiXinUser;
import com.soft.mainsite.module.wx.service.WeiXinUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: hjm
 * @Date: 2021/1/1 19:55
 * Stay hungry,stay foolish!
 */
@Slf4j
@Controller
@RequestMapping("/weiXinUser")
public class WeiXinUserController {

    @Resource
    private WeiXinUserService weiXinUserService;

    /**
     * 保存微信用户
     *
     * @param weiXinUser
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String saveMessage(WeiXinUser weiXinUser){
        if (StringUtils.isEmpty(weiXinUser.getId())){
            return weiXinUserService.save(weiXinUser);
        }
        return weiXinUserService.update(weiXinUser);
    }

    /**
     * 更改状态 (状态，0：未联系，1：已联系)
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateStatus")
    public String updateStatus(@RequestParam(value = "ids", required = false) Long[] ids){
        return weiXinUserService.updateStatus(ids);
    }

    /**
     * 查询微信用户列表（分页查询）
     *
     * @param pageNumber
     * @param pageSize
     * @param weiXinUser
     * @param model
     * @return
     */
    @RequestMapping("/getList")
    public String getMessageList(@RequestParam(value = "pageCurrent", defaultValue = "1") Integer pageNumber,
                                 @RequestParam(value = "pageSize", defaultValue = "30") Integer pageSize,
                                 WeiXinUser weiXinUser, Model model){

        model.addAttribute("pojo", weiXinUser);
        model.addAttribute("weiXinUserList", weiXinUserService.page(pageNumber, pageSize, weiXinUser));
        return "message/message_list";
    }

    /**
     * 删除微信用户信息（物理删除）
     *
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteMessage(@RequestParam(value = "ids", required = false) Long[] ids){
        return weiXinUserService.deleteMessage(ids);
    }

    /**
     * 导出微信用户信息
     *
     * @param pageNumber
     * @param pageSize
     */
    @RequestMapping("/excel")
    public void excel(@RequestParam(value = "pageCurrent",defaultValue = "1") Integer pageNumber,
                      @RequestParam(value = "pageSize",defaultValue = "30")Integer pageSize) {
        WeiXinUser weiXinUser = new WeiXinUser();
        PageInfo<WeiXinUser> pageInfo = weiXinUserService.page(pageNumber, pageSize, weiXinUser);
        ExcelUtil.exports2007("微信用户信息", pageInfo.getList());
    }
}
